package com.think.db.mongo.domain;

import dev.morphia.annotations.Collation;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.IndexOptions;
import dev.morphia.annotations.Indexed;
import dev.morphia.annotations.PreLoad;
import lombok.Data;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 玩家账号实体
 *
 * @author veione
 * @version 1.0
 */
@Data
@Entity(value = "accounts", useDiscriminator = false)
public class Account {
    @Id
    private String id;

    @Indexed(options = @IndexOptions(unique = true))
    @Collation(locale = "simple", caseLevel = true)
    private String username;

    private String password;

    private int reservedPlayerId;
    private String email;

    private String token;
    private String sessionKey;
    private List<String> permissions;
    private Locale locale;

    private String banReason;
    private int banEndTime;
    private int banStartTime;
    private boolean isBanned;

    public Account() {
        this.permissions = new ArrayList<>();
        this.locale = Locale.getDefault();
    }

    @PreLoad
    public void onLoad(Document doc) {
        if (doc.containsKey("locale")) {
            this.locale = new Locale(doc.getString("locale"));
        }
    }

    @Override
    public String toString() {
        return "Account ID: %s; Username: %s".formatted(id, username);
    }
}
